package com.lc.qianrenshiye.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.ShopCarShowGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/11/27.
 */
public class ShopCarAdapter extends ArrayAdapter<ShopCarShowGet.ShopCarEntity> {

    private Context context;
    private List<ShopCarShowGet.ShopCarEntity> shopCarLists;
    private LayoutInflater layoutInflater;
    private AddOrMinusListener addOrMinusListener;

    public ShopCarAdapter(Context context, List<ShopCarShowGet.ShopCarEntity> shopCarLists, AddOrMinusListener addOrMinusListener) {
        super(context, 0, shopCarLists);

        layoutInflater = LayoutInflater.from(context);

        this.context = context;

        this.shopCarLists = shopCarLists;

        this.addOrMinusListener = addOrMinusListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ShopCarShowGet.ShopCarEntity shopCarEntity = getItem(position);

        ViewHolder viewHolder;

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_home_shop_car_list, null)));

            convertView.setTag(viewHolder);
        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.shop_car_add_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addOrMinusListener.add(position);
            }
        });

        viewHolder.shop_car_minus_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addOrMinusListener.minus(position);
            }
        });

        viewHolder.shop_car_goods_name_tv.setText(shopCarEntity.title);
        viewHolder.shop_car_count_tv.setText(shopCarEntity.num);
        viewHolder.shop_car_price_tv.setText(shopCarEntity.deprice);

        GlideLoader.getInstance().get(context, shopCarEntity.picurl, viewHolder.shop_car_list_iv, R.mipmap.img_holder, GlideLoader.TYPE_IMAGE_FILLET );

        return convertView;
    }

    class ViewHolder {

        @BoundView(R.id.shop_car_list_iv) private ImageView shop_car_list_iv;
        @BoundView(R.id.shop_car_add_iv) private ImageView shop_car_add_iv;
        @BoundView(R.id.shop_car_minus_iv) private ImageView shop_car_minus_iv;
        @BoundView(R.id.shop_car_goods_name_tv) private TextView shop_car_goods_name_tv;
        @BoundView(R.id.shop_car_count_tv) private TextView shop_car_count_tv;
        @BoundView(R.id.shop_car_price_tv) private TextView shop_car_price_tv;

    }

    public interface AddOrMinusListener {

        void add(int pos);

        void minus(int pos);

    }
}
